package logic

import (
	"api/pkg"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	if !pkg.MobileType(req.Mobile) {
		return &types.Response{
			Code: 400,
			Msg:  "手机号格式错误",
		}, nil
	}
	// 生成验证码
	code := pkg.NumSmsCode()
	// 发送验证码
	err = pkg.SmsCode(req.Mobile, code)
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
		}, nil
	}

	return &types.Response{
		Code: 200,
		Msg:  "短信发送登录成功",
	}, nil
}
